<?php
/**
 *    Copyright 2010 Show Framework(www.showframework.cn)
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/**
 * 系统错误类
 * 错误是消息的一种
 * 
 * @package Show
 * @name Show_Error
 * @author show
 * @since v0.6
 */
class Show_Error
{
    /**
     * 错误信息
     *
     * @var string
     */
    protected $message;
    
    /**
     * 构造函数
     * 实例化一个错误
     *
     * @param string $message 错误信息
     * @return void
     * @since v0.6
     */
    public function __construct($message)
    {
        $this->message = $message;
    }
    
    /**
     * 把 Show_Error 类转换成字符串
     * 使用方法为exit(new Show_Error('message')); 或者echo new Show_Error('message');
     *
     * @return string 错误信息
     * @since v0.6
     */
    public function __toString()
    {
        $html = sprintf('
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<head>
<!-- author wangsibo wangsibo1988@gmail.com -->
<title>Error - Show Framework</title>
<style type="text/css">
*{
    padding:0px;
    margin:0px;
    border:0px;
}
body{
    text-align:center;
    font-size:12px;
}
#wrapper{
    width:840px;
    margin:auto;
    text-align:left;
}
#errorReport
{
    border:1px solid blue;
    width:838px;
    margin:50px 0 0;
    padding:20px;
}
#errorReport code
{
    color:black;
}
</style>
</head>
<body>
    <div id="wrapper">
        <div id="errorReport">
            <code>%s</code>
        </div>
    </div>
</body>
</html>', nl2br(str_replace(' ', '&nbsp;', $this->message)));
        return $html;
    }
    
    /**
     * 获取错误信息
     *
     * @return string 错误信息
     * @since v0.6
     */
    public function getMessage()
    {
        return $this->message;
    }
    
    /**
     * 致命错误
     * 调用此方法输出错误信息并终断程序
     *
     * @param string $message 错误信息
     * @return void
     * @since v0.6
     */
    public static function fatal($message)
    {
        exit(new Show_Error($message));
    }
}
?>